<template>
  <el-dialog
    v-dialogDrag
    :fullscreen="isFullscreen"
    :before-close="closeForm"
    width="4rem"
    v-if="dialogVisible"
    :visible.sync="dialogVisible"
  >
    <div slot="title" class="header-title hd-title">
      <span>{{ dialogTitle }}</span>
      <i
        v-if="isFullscreen === false"
        class="el-icon-full-screen"
        @click="fScreen"
      ></i>

      <i v-else class="el-icon-news" @click="ifScreen"></i>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="姓名：" prop="name">
            <el-input
              v-model="form.name"
              autocomplete="off"
              placeholder="请输入姓名"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="联系电话：" prop="Telephone">
            <el-input
              v-model="form.Telephone"
              autocomplete="off"
              placeholder="请输入联系电话"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="邮编：" prop="postal_code">
            <el-input
              v-model="form.postal_code"
              autocomplete="off"
              placeholder="请输入邮编"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <!--          @submitArea="changeArea"-->
          <el-form-item label="所在区域：" prop="address_one">
            <ChinaArea
              ref="addressForm"
              :optionData.sync="form.address_one"
            ></ChinaArea>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="详细地址：" prop="address_two">
            <el-input
              v-model="form.address_two"
              autocomplete="off"
              placeholder="请输入联系电话"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="closeForm">取 消</el-button>
      <el-button size="small" type="primary" @click="handleSubmit"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "addressModal",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    dialogTitle: {
      type: String,
      default: "",
    },
    editData: {
      type: Object,
      default: {},
    },
  },
  watch: {
    editData: {
      handler(val) {
        if (val) {
          this.form = JSON.parse(JSON.stringify(val));
        }
      },
    },
  },
  data() {
    return {
      form: {
        address_one: [],
      },
      formLabelWidth: "80px",
      isFullscreen: false,
      rules: {
        name: [
          {
            required: true,
            message: "请输入姓名",
            trigger: ["blur", "change"],
          },
        ],
        Telephone: [
          {
            required: true,
            message: "请输入电话号码",
            trigger: ["blur", "change"],
          },
        ],
        address_one: [
          {
            required: true,
            message: "请选择所在区域",
            trigger: ["blur", "change"],
          },
        ],
        address_two: [
          {
            required: true,
            message: "请输入详细地址",
            trigger: ["blur", "change"],
          },
        ],
        postal_code: [
          {
            required: true,
            message: "请输入邮编",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.address_one) {
            this.$emit("submit", this.form);
            this.$emit("closeForm");
          } else {
            this.$message({
              type: "warning",
              message: "请选择区域",
            });
          }
        }
      });
    },
    closeForm() {
      this.$emit("closeForm");
      this.isFullscreen = false;
      if (this.$refs["form"] !== undefined) {
        this.$refs["form"].resetFields();
      }
    },
    fScreen() {
      this.isFullscreen = true;
    },
    ifScreen() {
      this.isFullscreen = false;
    },
  },
};
</script>

<style scoped lang="scss">
.el-form-item {
  margin-bottom: 8px;
}

::v-deep .el-form-item__content {
  line-height: 14px;
}

::v-deep .el-dialog__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
}

::v-deep .el-input__inner {
  height: 32px;
  line-height: 32px;
}

::v-deep .el-dialog__body {
  padding: 20px 20px 0 20px;
}

::v-deep .el-dialog__footer {
  padding: 10px 16px;
  border-top: 1px solid #f0f0f0;
}

.el-select-dropdown
  .el-scrollbar
  .el-scrollbar__view
  .el-select-dropdown__item {
  height: auto;
  padding: 0 15px;
}

.hd-title {
  font-size: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex: 1;
  margin-right: 26px;
}

.el-icon-full-screen {
  cursor: pointer;
}

.el-icon-news {
  cursor: pointer;
}

::v-deep .el-form-item__label {
  padding: 0;
  text-align: left;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
